import { ref } from 'vue'
import { templateGetDetail, templateSave } from '@/api/basic'

const useForm = () => {
  // 表单
  const form = ref({
    delTemplateModuleIds: [],
    id: null,
    name: '',
    status: false,
    templateField: [],
    templateModuleIds: [],
  })

  // 保存加载
  const saveLoading = ref(false)

  /**
   * 保存方法
   */
  const saveFn = async () => {
    saveLoading.value = true
    const res = await templateSave(form.value)
    saveLoading.value = false
    return res.data as { code: number; message: string; data: object; success: boolean }
  }

  // 获取表单加载
  const getFormLoading = ref(false)

  /**
   * 获取表单内容并填充到表单里
   * @param {string} id - 工单模块id
   */
  const getFormFn = async (id: string) => {
    getFormLoading.value = true
    const res = await templateGetDetail(id)
    form.value = res.data.data.row
    getFormLoading.value = false
  }

  return {
    form,
    saveLoading,
    getFormLoading,
    saveFn,
    getFormFn,
  }
}

export default useForm
